package fichier;
import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

/**
 * La Classe PlanPanneau crée et affiche le plan de l'entrepot, et d'indiquer l'emplacement du modele.
 * @author Rinca
 *
 */
@SuppressWarnings("serial")
public class PlanPanneau extends JPanel{
	
	private final int XLEFT = 150;
	private final int XMID = 440;
	private final int XRIGHT = 700;
	private final int YTOP = 20;
	private final int YMID = 328;
	private final int YDOWN = 580;
	
	private final int EPEXT = 3;
	private final int EPIN = 2;
	
	private final int PBOX = 85;
	private final int LBOXAD = 18;
	private final int LBOXAG = 19;
	private final int LBOXAH = 18;
	private final int LBOXPDH = 22;
	private final int LBOXPGH = LBOXPDH;
	private final int LBOXPDB = 40;
	private final int LBOXPGB = 32;
	
	private String nomBox;
	@SuppressWarnings("unused")
	private String nomModele;
	
	/**
	 * Instancie le plan de l'entrepot.
	 * @param nom
	 * @param box
	 */
	public PlanPanneau(String nom,String box){
		super();
		this.nomBox = box;
		this.nomModele = nom;
	}
	
	/**
	 * Appel les diffenrent elements de construction du plan de l'entrepot.
	 */
	public void paintComponent(Graphics g){
		
		this.afficheBox(g);
		this.plan(g);
		this.nomBox(g);
	
		
	}
	
	private void plan(Graphics g){
		g.setColor(new Color(50,50,50));
		//les contours
		g.fillRect(this.XLEFT,this.YTOP,this.XRIGHT-this.XLEFT,this.EPEXT);
		g.fillRect(this.XLEFT,this.YTOP,this.EPEXT,this.YDOWN-this.YTOP+this.EPIN-2*this.LBOXPGB);
		g.fillRect(this.XLEFT, this.YDOWN-this.LBOXPGB, this.EPEXT, this.LBOXPGB);
		g.fillRect(this.XLEFT,this.YDOWN,this.XMID-this.XLEFT+this.EPEXT,this.EPEXT);
		g.fillRect(this.XRIGHT,this.YTOP,this.EPEXT,this.YMID-this.YTOP);
		g.fillRect(this.XMID,this.YMID,this.XRIGHT-this.XMID+this.EPEXT,this.EPEXT);
		g.fillRect(this.XMID,this.YMID,this.EPEXT,this.YDOWN-this.YMID+this.EPEXT);
		g.fillRect(this.XMID, this.YTOP, this.EPEXT, this.YMID-this.YTOP-this.LBOXPDH+this.EPIN);
		
		//arriere salle rang� droite
		for(int i=0;i<=16;i++){
			g.fillRect(this.XRIGHT-this.PBOX, this.YTOP+this.LBOXAD*i, this.PBOX, this.EPIN);
			//g.fillRect(this.XRIGHT, this.YTOP+23*i, this.LBOXAD, this.EPIN);
		}
		
		//arriere salle rang� gauche
		for(int i=0; i<=14;i++){
			g.fillRect(this.XMID+this.EPEXT, this.YTOP+this.LBOXAG*i, this.PBOX, this.EPIN);
			
		}
		
		//arriere salle rang� haut
		for(int i=0; i<=2; i++){
			g.fillRect((int)this.XMID+((this.XRIGHT-this.XMID)*i/3), this.YTOP, this.EPIN, this.LBOXAH);
		}
		
		//premiere salle rang� droite haut
		for(int i=0; i<=16; i++){
			g.fillRect(this.XMID-this.PBOX, this.YTOP+this.LBOXPDH*i, this.PBOX, this.EPIN);
		}
		
		//premeire salle rang� gauche haut
		for(int i=0; i<=16; i++){
			g.fillRect(this.XLEFT+this.EPEXT, this.YTOP+this.LBOXPGH*i, this.PBOX, this.EPIN);
		}
		
		//premiere salle rang� gauche bas
		for(int i=0; i<=5; i++){
			g.fillRect(this.XLEFT+this.EPEXT, this.YDOWN-this.LBOXPGB*i, this.PBOX, this.EPIN);
		}
		
		//premiere salle rang� droite bas
		for(int i=0; i<=4; i++){
			g.fillRect(this.XMID-this.PBOX, this.YDOWN-this.LBOXPDB*i, this.PBOX, this.EPIN);
		}
		
	}
	
	/**
	 * Ecrit le nom des box dans chacun d'eux.
	 * @param g
	 */
	private void nomBox(Graphics g){
		g.setColor(new Color(50,50,50));
		//arriere salle rang� gauche
		char boxAG[]={'L','K','J','I','H','G','F','E','D','C','B','A','P'};
		for(int i=0; i<13; i++){
			g.drawString(boxAG[i]+" 1-8",this.XMID+((this.PBOX/2)-8),this.LBOXAG*(i+2)+this.YTOP-3);
		}
		
		//arriere salle rang� haut
		char boxAH[]={'M','N'};
		for(int i=0; i<2; i++){
			g.drawString(boxAH[i]+" 1-8",(int)this.XMID+(((this.XRIGHT-this.XMID)*i/3)+25),this.LBOXAG+this.YTOP-3);
		}
		
		//arriere salle rang� droite
		g.drawString("O 1,2,3,5",this.XRIGHT-((this.PBOX/2)+29),this.LBOXAD*2+this.YTOP-2);
		g.drawString("R 1,2 S 1,2",this.XRIGHT-((this.PBOX/2)+38),this.LBOXAD*3+this.YTOP-2);
		g.drawString("Niche 1,2,3",this.XRIGHT-((this.PBOX/2)+42),this.LBOXAD*4+this.YTOP-2);
		for(int i=1; i<=12; i++){
			g.drawString("T "+i,this.XRIGHT-((this.PBOX/2)+5),this.LBOXAD*(i+4)+this.YTOP-2);
		}
		
		//premiere salle rang� gauche haut
		for(int i=0; i<16; i++){
			g.drawString(""+(16-i)*2, this.XLEFT+(this.PBOX/2)-2, this.YTOP+this.LBOXPGH*(i+1)-2);
		}
		
		//premiere salle rang� droite haut
		for(int i=0; i<12; i++){
			int str = ((16-i)*2)-1;
			g.drawString(""+str+" et bis", this.XMID-(this.PBOX/2+29), this.YTOP+this.LBOXPGH*(i+1)-2);
		}
		g.drawString("5/7"+" et bis", this.XMID-(this.PBOX/2+32), this.YTOP+this.LBOXPGH*13-2);
		for(int i=0; i<2; i++){
			int str = ((2-i)*2)-1;
			g.drawString(""+str+" et bis", this.XMID-(this.PBOX/2+29), this.YTOP+this.LBOXPGH*(i+15)-2);
		}
		
		//premiere salle rang� gauche bas
		g.drawString("Départ", this.XLEFT+(this.PBOX/2-17), this.YDOWN-this.LBOXPGB*5-17);
		g.drawString("moulage", this.XLEFT+(this.PBOX/2-22), this.YDOWN-this.LBOXPGB*5-4);
		for(int i=0; i<3; i++){
			int str = 34+i*2;
			g.drawString(""+str, this.XLEFT+(this.PBOX/2-3), this.YDOWN-this.LBOXPGB*(i+2)-8);
		}
		g.drawString("A", this.XLEFT+(this.PBOX/2-1), this.YDOWN-18);
		g.drawString("détruire", this.XLEFT+(this.PBOX/2-17), this.YDOWN-5);
		
		//premiere salle rang� droite bas
		for(int i=0; i<3; i++){
			int str = 34+i*2-1;
			g.drawString(""+str, this.XMID-(this.PBOX/2+6), this.YDOWN-this.LBOXPDB*(i+2)-8);
		}
		g.drawString("Arrivée", this.XMID-(this.PBOX/2+19), this.YDOWN-this.LBOXPDB-17);
		g.drawString("modèle", this.XMID-(this.PBOX/2+22), this.YDOWN-this.LBOXPDB-4);
		g.drawString("A", this.XMID-(this.PBOX/2+3), this.YDOWN-17);
		g.drawString("expédier", this.XMID-(this.PBOX/2+24), this.YDOWN-4);
	}
	
	
	private void afficheBox(Graphics g){
		g.setColor(Color.yellow);
		int tab[] = this.traitementEmplacement();
		g.fillRect(tab[0], tab[1], tab[2], tab[3]);
	}
	
	private int[] traitementEmplacement(){
		int tab[] = {0,0,0,0};
		int idZone = -1, idBox = -1;
		
		/**
		 * ici traiment des coordonnées du box
		 */
		
		//initialisation des nom de box
		ArrayList<ArrayList<ArrayList<String>>> box = new ArrayList<ArrayList<ArrayList<String>>>();
		
		//initialisation arriere salle rang�e droite
		box.add(new ArrayList<ArrayList<String>>());
			box.get(0).add(new ArrayList<String>());
				box.get(0).get(0).add("O 1");
				box.get(0).get(0).add("O 2");
				box.get(0).get(0).add("O 3");
				box.get(0).get(0).add("O 5");
			box.get(0).add(new ArrayList<String>());
				box.get(0).get(1).add("R 1");
				box.get(0).get(1).add("R 2");
				box.get(0).get(1).add("S 1");
				box.get(0).get(1).add("S 2");
			box.get(0).add(new ArrayList<String>());
				for(int i=0; i<3; i++){
					box.get(0).get(2).add("Niche "+(i+1));
				}
			
				for(int i=0; i<12; i++){
			box.get(0).add(new ArrayList<String>());
					box.get(0).get(i+3).add("T "+(i+1));
				}
		
		//initialisation arriere salle rang�e gauche
		box.add(new ArrayList<ArrayList<String>>());
			String carac[]={"L","K","J","I","H","G","F","E","D","C","B","A","P"};
			for(int i=0; i<13; i++){
			box.get(1).add(new ArrayList<String>());
				for(int j=0; j<8; j++){
					box.get(1).get(i).add(""+carac[i]+" "+(j+1));
				}
			}
			
		
		//initialisation arriere salle rang�e haut
		box.add(new ArrayList<ArrayList<String>>());
			String carac2[]={"M","N"};
			for(int i=0; i<2; i++){
			box.get(2).add(new ArrayList<String>());
				for(int j=0; j<8; j++){
					box.get(2).get(i).add(""+carac2[i]+" "+(j+1));
				}
			}
		
		//initialisation premiere salle rang�e droite haut
		box.add(new ArrayList<ArrayList<String>>());
			for(int i=0; i<13; i++){	
				box.get(3).add(new ArrayList<String>());
				box.get(3).get(i).add(""+(31-(i*2)));
				box.get(3).get(i).add(""+(31-(i*2))+" bis");
			}
			//box.get(3).add(new ArrayList<String>());
			box.get(3).get(12).add("5");
			box.get(3).get(12).add("5 bis");
			box.get(3).add(new ArrayList<String>());
			box.get(3).get(13).add("");
			box.get(3).add(new ArrayList<String>());
			for(int i=0; i<2; i++){
				box.get(3).add(new ArrayList<String>());
				box.get(3).get(i+14).add(""+(3-(i*2)));
				box.get(3).get(i+14).add(""+(3-(i*2))+" bis");
			}
			
		
		//initialisation premiere salle rang�e gauche haut
		box.add(new ArrayList<ArrayList<String>>());
			for(int i=0; i<16; i++){
				box.get(4).add(new ArrayList<String>());
				box.get(4).get(i).add(""+(32-(i*2)));
			}
		
		//initialisation premiere salle rang�e droite bas
		box.add(new ArrayList<ArrayList<String>>());
			box.get(5).add(new ArrayList<String>());
				box.get(5).get(0).add("A expedier");
			box.get(5).add(new ArrayList<String>());
				box.get(5).get(1).add("Arrivee modele");
			for(int i=0; i<3; i++){
				box.get(5).add(new ArrayList<String>());
				box.get(5).get(i+2).add(""+(i*2+33));
			}
		
		//initialisation premiere salle rang�e gauche bas
		box.add(new ArrayList<ArrayList<String>>());
			box.get(6).add(new ArrayList<String>());
				box.get(6).get(0).add("A detruire");
			box.get(6).add(new ArrayList<String>());
				box.get(6).get(1).add("");
			for(int i=0; i<3; i++){
				box.get(6).add(new ArrayList<String>());
				box.get(6).get(i+2).add(""+((2*i)+34));
			}
			box.get(6).add(new ArrayList<String>());
				box.get(6).get(5).add("Depart moulage");
		
		
		//comparaison avec le nom en parametre
		for(int i=0; i<box.size(); i++){
			for(int j=0; j<box.get(i).size(); j++){
				for(int k=0; k <box.get(i).get(j).size(); k++){
					if(nomBox.equals(box.get(i).get(j).get(k))){
						idZone = i+1;
						idBox = j+1;
					}
				}
			}
		}
		
		
		//configuration des coordonn�es
		if(idZone > 0 && idBox > 0){
			switch(idZone){
				//arriere salle rang�e droite
				case 1:
					tab[0]= this.XRIGHT-this.PBOX;
					tab[1]= this.YTOP+this.LBOXAD*idBox;
					tab[2]= this.PBOX;
					tab[3]= this.LBOXAD;
				break;
				//arriere salle rang�e gauche
				case 2:
					tab[0] = this.XMID+this.EPEXT;
					tab[1] = this.YTOP+this.LBOXAG*idBox;
					tab[2] = this.PBOX;
					tab[3] = this.LBOXAG;
				break;
				//arriere salle rang�e haut
				case 3:
					tab[0] = (int)this.XMID+((this.XRIGHT-this.XMID)*(idBox-1)/3);
					tab[1] = this.YTOP;
					tab[2] = (this.XRIGHT-this.XMID)/3;
					tab[3] = this.LBOXAH;
				break;
				//premiere salle rang�e droite haut
				case 4:
					tab[0] = this.XMID-this.PBOX;
					tab[1] = this.YTOP+this.LBOXPDH*(idBox-1);
					tab[2] = this.PBOX;
					tab[3] = this.LBOXPDH;
				break;
				//premiere salle rang�e gauche haut
				case 5:
					tab[0] = this.XLEFT+this.EPEXT;
					tab[1] = this.YTOP+this.LBOXPGH*(idBox-1);
					tab[2] = this.PBOX;
					tab[3] = this.LBOXPGH;
				break;
				//premiere salle rang�e droite bas
				case 6:
					tab[0] = this.XMID-this.PBOX;
					tab[1] = this.YDOWN-this.LBOXPDB*idBox;
					tab[2] = this.PBOX;
					tab[3] = this.LBOXPDB;
				break;
				//premiere salle rang�e gauche bas
				case 7:
					tab[0] = this.XLEFT+this.EPEXT;
					tab[1] = this.YDOWN-this.LBOXPGB*idBox;
					tab[2] = this.PBOX;
					tab[3] = this.LBOXPGB;
				break;
			}
		}
		else{
			tab[0]=0;
			tab[1]=0;
			tab[2]=0;
			tab[3]=0;
		}
		
		
		return tab;
	}

}
